package com.hengda.museumonline.adapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hengda.museumonline.R;
import com.hengda.museumonline.entity.NewsBean;
import com.hengda.museumonline.ui.NewsContentActivity;

/**
 * @author：JFWU on 2016/9/26 18:36
 * @E-mail：jeffy12138@126.com
 */
public class RelationNewsAdapter extends RecyclerView.Adapter<RelationNewsAdapter.ListHolder> {
    private NewsContentActivity context;
    private NewsBean newsBean;

    public RelationNewsAdapter(NewsContentActivity context, NewsBean newsBean) {
        this.context = context;
        this.newsBean = newsBean;
    }

    @Override
    public RelationNewsAdapter.ListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.relationnews_item, null);
        return new ListHolder(view);
    }

    @Override
    public void onBindViewHolder(RelationNewsAdapter.ListHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return newsBean.getData().getRelationNews().size();
    }

    class ListHolder extends RecyclerView.ViewHolder {
        private TextView postTitle;
        private TextView postModified;
        private LinearLayout linearLayout;

        public ListHolder(View itemView) {
            super(itemView);
            postTitle = (TextView) itemView.findViewById(R.id.post_title);
            postModified = (TextView) itemView.findViewById(R.id.post_modified);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.ll);
        }

        public void setData(final int position) {
            postTitle.setText(newsBean.getData().getRelationNews().get(position).getPost_title());
            postModified.setText(newsBean.getData().getRelationNews().get(position).getPost_modified());
            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putString("object_id",newsBean.getData().getRelationNews().get(position).getObject_id());
                    Intent intent = new Intent(context, NewsContentActivity.class);
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                    context.finish();
                }
            });
        }
    }
}
